﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAO;
using DTO;
using PTExpress.Models;

namespace PTExpress.Controllers
{
    [Authorize]
    public class QLKhachHangController : Controller
    {

        public Dictionary<int, string> GioiTinh;

        public QLKhachHangController()
            : base()
        {
            GioiTinh = new Dictionary<int, string>();
            GioiTinh.Add(1, "Nam");
            GioiTinh.Add(0, "Nữ");
            this.ViewData.Add("GioiTinh", GioiTinh);

            IEnumerable<SelectListItem> lsEGioiTinh = KhachHangDTO.GetListGioiTinh();
            this.ViewData.Add("lsEGioiTinh", lsEGioiTinh);
        }
        //
        // GET: /KhachHang/

        public ActionResult Index()
        {
            KhachHangDAO khachhangDAO = new KhachHangDAO();
            List<KhachHangDTO> ls = khachhangDAO.GetAllUnCommited();
            this.ViewData.Add("dsKhachHang", ls);
            return View();
        }

        //
        // GET: /KhachHang/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /KhachHang/Create

        public ActionResult Them()
        {
            return View();
        } 

        //
        // POST: /KhachHang/Create

        [HttpPost]
        public ActionResult Them(KhachHang model)
        {
                ViewData["ModelState"] = ModelState.IsValid;

                bool thanhCong = false;
                if (ModelState.IsValid)
                {
                    KhachHangDAO khachhangDAO = new KhachHangDAO();
                    if (khachhangDAO.CheckUsername(model.Tendangnhap))
                    {
                        ModelState.AddModelError("TenDangNhap", "Trùng tên đăng nhập, vui lòng chọn một tên khác chưa sử dụng.");
                        ViewData["ModelState"] = false;
                        return View(model);
                    }

                    if (khachhangDAO.CheckEmail(model.Email))
                    {
                        ModelState.AddModelError("Email", "Trùng email, vui lòng chọn một email khác chưa sử dụng.");
                        ViewData["ModelState"] = false;
                        return View(model);
                    }

                    if (khachhangDAO.CheckCMND(model.Cmnd))
                    {
                        ModelState.AddModelError("CMND", "Trùng CMND, vui lòng chọn một CMND khác chưa sử dụng.");
                        ViewData["ModelState"] = false;
                        return View(model);
                    }
                    KhachHangDTO khachhangDTO = new KhachHangDTO();
                    khachhangDTO.Tendangnhap = model.Tendangnhap;
                    khachhangDTO.Matkhau = model.Matkhau;
                    khachhangDTO.Email = model.Email;
                    khachhangDTO.Hoten = model.Hoten;
                    khachhangDTO.Cmnd = model.Cmnd;
                    khachhangDTO.Gioitinh = model.Gioitinh;
                    khachhangDTO.Ngaydangky = DateTime.Now;
                    khachhangDTO.No = 0.0;
                    thanhCong = khachhangDAO.AddNew(khachhangDTO);
                    if (thanhCong)
                    {
                        return RedirectToAction("Index", "QLKhachHang");
                    }
                }
                ViewData["ThanhCong"] = thanhCong;
                return View(model);
        }
        
        //
        // GET: /KhachHang/Edit/5
 
        public ActionResult Sua(int id)
        {
            KhachHang model = new KhachHang();
            KhachHangDAO khachhangDAO = new KhachHangDAO();
            KhachHangDTO khachhangDTO = khachhangDAO.getById(id);
            ViewData["HoTen"] = khachhangDTO.Hoten;
            model.Id = id;
            model.Tendangnhap = khachhangDTO.Tendangnhap;
            model.Matkhau = khachhangDTO.Matkhau;
            model.Email = khachhangDTO.Email;
            model.Hoten = khachhangDTO.Hoten;
            model.Cmnd = khachhangDTO.Cmnd;
            model.Gioitinh = khachhangDTO.Gioitinh;
            model.No = khachhangDTO.No;

            return View(model);
        }

        //
        // POST: /KhachHang/Edit/5

        [HttpPost]
        public ActionResult Sua(int id, KhachHang model)
        {
            KhachHangDAO khachhangDAO = new KhachHangDAO();
            KhachHangDTO khachhangDTO = khachhangDAO.getById(id);
            ViewData["HoTen"] = khachhangDTO.Hoten;
            ViewData["ModelState"] = ModelState.IsValid;

            bool thanhCong = false;
            if (ModelState.IsValid)
            {
                khachhangDTO.Tendangnhap = model.Tendangnhap;
                khachhangDTO.Matkhau = model.Matkhau;
                khachhangDTO.Email = model.Email;
                khachhangDTO.Cmnd = model.Cmnd;
                khachhangDTO.Gioitinh = model.Gioitinh;
                khachhangDTO.No = model.No;
                khachhangDTO.Hoten = model.Hoten;
                thanhCong = khachhangDAO.Edit(khachhangDTO);
            }
            ViewData["ThanhCong"] = thanhCong;
            return View(model);
        }

        //
        // GET: /KhachHang/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /KhachHang/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
